<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Models;

use DateTimeInterface;
use Illuminate\Bus\Batch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Jiaxincui\ClosureTable\Traits\ClosureTable;

class User extends Model
{
    use ClosureTable;
    use Notifiable;
    use \App\Traits\ModelTraits\LogsActivityTrait;

    //protected $table = '';
    //protected $primaryKey='';
    //public $incrementing = false //使用非递增或者非数字的主键设置false
    //public $timestamps = false; //关闭自动时间维护
    //protected $fillable = ['']; //可被批量赋值
    protected $guarded = []; //不被批量赋值 空为全部可以赋值

    //protected $dates = ['deleted_at'];
    //protected $hidden = ['user_password'];
    protected $closureTable = 'user_closures';

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];

    /**
     * 积分账户.
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function pointAccount()
    {
        return $this->hasOne(PointAccount::class, 'user_id', 'id');
    }

    /**
     * KC通证账户.
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function beanAccount()
    {
        return $this->hasOne(GoldBeanAccount::class, 'user_id', 'id');
    }

    /**
     * 钱包账户.
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function walletAccount()
    {
        return $this->hasOne(WalletAccount::class, 'user_id', 'id');
    }

    /**
     * 奖励余额账户.
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function rewardAccount()
    {
        return $this->hasOne(UserRewardAccount::class, 'user_id', 'id');
    }

    public function consumption()
    {
        return $this->hasMany(ProductOrder::class, 'user_id', 'id')
            ->where('status', '>', 1)
            ->select('total_amount', 'user_id', 'id');
    }

    public function performanceLevel()
    {
        return $this->hasOne(PerformanceLevel::class, 'id', 'agent_level');
    }

    public function teamMember()
    {
        return $this->hasOne(PerformanceMember::class, 'user_id', 'id');
    }

    public function address()
    {
        return $this->hasOne(UserAddress::class, 'user_id', 'id')->where('is_default', 1);
    }

    public function notifications()
    {
        return $this->morphMany(Notifications::class, 'notifiable')->orderBy('created_at', 'desc');
    }

    public function userLevel()
    {
        return $this->belongsTo(UserLevelSetting::class, 'member_level', 'id');
    }

    public function productOrder()
    {
        return $this->hasMany(ProductOrder::class, 'user_id', 'id');
    }

    public function GroupOrder()
    {
        return $this->hasMany(GroupOrderBatche::class, 'user_id', 'id');
    }

    public function totalRevenue() //收益
    {
        return $this->hasOne(UserTotalRevenue::class, 'user_id', 'id');
    }

    public function buyTrades()
    {
        return $this->hasMany(CreditTrade::class, 'buyer_id');
    }

    public function sellTrades()
    {
        return $this->hasMany(CreditTrade::class, 'seller_id');
    }

    public function bank_card()
    {
        return $this->hasMany(UserBankCard::class, 'id', 'default_bank_card_id');
    }

    public function signRecords()
    {
        return $this->hasMany(SignRecord::class, 'user_id', 'id');
    }

    public function GroupOrderCount()
    {
        return $this->hasMany(GroupOrder::class, 'user_id', 'id')->select('id', 'user_id');
    }

    public function mYparent()
    {
        return $this->hasOne(User::class, 'id', 'parent');
    }

    public function OrderBeanReward()
    {
        return $this->hasMany(OrderBeanRewards::class, 'user_id', 'id');
    }

    public function voucher()
    {
        return $this->hasMany(VoucherPackage::class, 'user_id', 'id');
    }

    public function batches()
    {
        return $this->hasMany(OrderBatch::class, 'user_id', 'id');
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}
